package edu.hda.pt.server;

import java.io.IOException;

import edu.hda.pt.server.connections.PTServerMINA;
import edu.hda.pt.server.connections.PTServerNetty;
import edu.hda.pt.server.connections.PTServerRMI;
import edu.hda.pt.server.connections.PTServerXSocket;
import edu.hda.pt.utils.ConnectionMethod;
import edu.hda.pt.utils.Logger;

/**
 * RunPTServer is the main class for running the server
 * 
 * @author Marc Hammerton
 * 
 */
public class RunPTServer {

	/**
	 * Main method for running the server. It takes at least two arguments:
	 * first the connection method to use and second the level for the logger
	 * details. If necessary, method specific arguments can also be used. It
	 * then creates the connection method specific server and starts it. It then
	 * waits for an user input to stop the server.
	 * 
	 * @param args
	 *            - [0] the connection method; [1] level for the logger details
	 *            [2+] connection method specific arguments
	 */
	public static void main(String[] args) {

		if (args.length < 2) {
			System.out.println("Error: Wrong number of arguments");
			System.out
					.println("Usage: RunPTServer <METHOD> <LOGGER_DETAILS> {<METHODSPECIFIC>}");
			System.exit(1);
		}

		PTServer server = null;
		if (args[0].equals(ConnectionMethod.RMI.toString())) {
			server = new PTServerRMI(args);
		} else if (args[0].equals(ConnectionMethod.MINA.toString())) {
			server = new PTServerMINA();
		} else if (args[0].equals(ConnectionMethod.NETTY.toString())) {
			server = new PTServerNetty();
		} else if (args[0].equals(ConnectionMethod.XSOCKET.toString())) {
			server = new PTServerXSocket();
		} else {
			System.out.println("Error: connection method not supported");
		}

		server.serverID = "server_1";
		server.start();
		System.out.println("PTServer ready. Press <Enter> to quit");
		try {
			System.in.read();
		} catch (IOException e) {
			e.printStackTrace();
		}

		Logger.printTestTimes();

		server.stop();
		System.exit(0);
	}

}
